package com.putao.album.message;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.putao.album.R;
import com.putao.album.base.BaseActivity;
import com.putao.album.db.MessageHelper;
import com.putao.album.db.tables.InMessage;
import com.putao.album.photodetail.ActivityPhotoDetail;
import com.putao.album.tinyalbum.ActivityTinyAlbum;
import com.putao.album.util.ActivityHelper;
import com.putao.album.util.CommonUtils;
import com.putao.album.util.Loger;
import com.putao.album.util.StringHelper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by chen on 15-5-4.
 * <p/>
 * MODIFY BY JIDONGDONG 5/14
 */
public class ActivityMessage extends BaseActivity {
    private Button back_btn;
    private Button right_btn;
    private TextView title_tv;
    private InMessageAdapter mAdapter;
    private List<InMessage> mList = new ArrayList<InMessage>();
    private ListView list_view;
    private LinearLayout no_message;
    private MessageHelper msghelp;

    @Override
    public int doGetContentViewId() {
        return R.layout.activity_message;
    }

    @Override
    public void doInitSubViews(View view) {
        title_tv = queryViewById(R.id.title_tv);
        title_tv.setText(R.string.message);
        back_btn = (Button) findViewById(R.id.back_btn);
        right_btn = (Button) findViewById(R.id.right_btn);
        right_btn.setBackgroundColor(getResources().getColor(R.color.transparent));
        right_btn.setText(getString(R.string.clear));
        right_btn.setTextColor(getResources().getColor(R.color.blue));
        right_btn.setVisibility(View.VISIBLE);
        no_message = queryViewById(R.id.no_message);
        list_view = queryViewById(R.id.list_view);
        list_view.setOnItemClickListener(itemClickListener);
        addOnClickListener(back_btn, right_btn);
    }


    AdapterView.OnItemClickListener itemClickListener = new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            final InMessage message = mList.get(position);
            if (message == null) {
                return;
            }
            Loger.d("album_id:" + message.getAlbum_id() + ",phpto_id:" + message.getPhoto_id());
            //go相册
            if (!StringHelper.isEmpty(message.getAlbum_id())) {
                Bundle bundle = new Bundle();
                bundle.putString("albumId", message.getAlbum_id());
                ActivityHelper.startActivity(mActivity, ActivityTinyAlbum.class, bundle);
            }
            //go照片
            if (!StringHelper.isEmpty(message.getPhoto_id())) {
                Bundle bundle = new Bundle();
                bundle.putStringArrayList("ids", new ArrayList<String>() {{
                    add(message.getPhoto_id());
                }});
                bundle.putInt("position", 0);
                ActivityHelper.startActivity(mActivity, ActivityPhotoDetail.class, bundle);
            }
            //标记已读
            message.setIsread(1);
            msghelp.updateInMessage(message, new HashMap<String, String>() {{
                put("id", String.valueOf(message.getId()));
            }});
            if (position > -1) {
                mList.get(position).setIsread(1);
                mAdapter.notifyDataSetChanged();
            }
        }
    };

    @Override
    public void doInitData() {
        msghelp = new MessageHelper(mContext);
        getData();
    }

    /**
     * 获取数据
     */
    private void getData() {
        List<InMessage> messageList = msghelp.getInMessageByWhere(new HashMap<String, String>() {{
            put("baby_id", CommonUtils.getBabyId());
        }});
        mList.clear();
        if (messageList != null) {
            Loger.d("size:" + messageList.size());
            mList.addAll(messageList);
        }

        mAdapter = new InMessageAdapter(mContext, mList);
        list_view.setAdapter(mAdapter);
        showNoMessageView();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back_btn:
//                flagRead();
                finish();
                break;
            case R.id.right_btn:
                clearMessage();
                break;
        }
    }

    private void showNoMessageView() {
        no_message.setVisibility(mList.size() > 0 ? View.GONE : View.VISIBLE);
    }

//    /**
//     * 标记已读
//     */
//    private void flagRead() {
//        for (int i = 0; i < mList.size(); i++) {
//            InMessage msg = mList.get(i);
//            final String id = String.valueOf(msg.getId());
//            if (msg.getIsread() == 0) {
//                msg.setIsread(1);
//                msghelp.updateInMessage(msg, new HashMap<String, String>() {{
//                    put("id", id);
//                }});
//            }
//        }
//        CommonUtils.setNewMessageFlag(false);
//    }

    /**
     * 清空消息
     */
    private void clearMessage() {
        for (int i = 0; i < mList.size(); i++) {
            InMessage msg = mList.get(i);
            final String id = String.valueOf(msg.getId());
            msghelp.deleteInMessage(new HashMap<String, String>() {{
                put("id", id);
            }});
        }
        mList.clear();
        if (mAdapter != null) {
            mAdapter.notifyDataSetChanged();
        }
        showNoMessageView();
        CommonUtils.setNewMessageFlag(false);
    }
}
